<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/17 0017
 * Time: 16:57
 */

namespace Admin\Controller;

use Admin\Model\Goods_classModel;
use Admin\Model\GoodsModel;


class GoodsClassController extends CommonController
{

    function index()
    {
        $cateM = new Goods_classModel();

        $cate = $cateM->getCateTree();

        $this->assign('list', $cate);
        $this->display();
    }

    public function add()
    {

        if (IS_POST) {
            $data = I('post.');


            //图片上传
            $img = new GoodsModel();
            $res = $img->uploadImg();

            if ($res) {
                $data['class_icon'] = "http://".$_SERVER['HTTP_HOST'].'/'.$res['goods_img'];
                $data['class_img'] = "http://".$_SERVER['HTTP_HOST'].'/'.$res['goods_thumb'];
            }

            //die(json_encode(array('rel'=>$data)));

            $rel = M('Goods_class')->add($data);

            if($rel){
                // header("location: http://www.dzshop.net/Admin/Index/index.html");
                $this->success("添加成功！",U("GoodsClass/index"),1);
            }else{
                $this->error("添加失败！");
            }

            //$this->success("成功",U('index'));
            //$this->redirect('GoodsClass/index');


        }


        $cateM = new Goods_classModel();

        $cate = $cateM->getCateTree();

        $this->assign('cate', $cate);
        $this->display();
    }

    public function edit(){
        if(IS_POST) {
            $data = I('post.');
            $map['name'] = $data['name'];
            $map['parent_class_id'] = $data['parent_class_id'];
            $map['state'] = $data['state'];
            $map['seo_title'] = $data['seo_title'];
            $map['seo_keywords'] = $data['seo_keywords'];
            $map['sort'] = $data['sort'];
            $map['seo_description'] = $data['seo_description'];

            if(isset($_FILES['goods_img'])){
                //图片上传
                $img = new GoodsModel();
                $res = $img->uploadImg();

                if ($res) {
                    $map['class_icon'] = "http://".$_SERVER['HTTP_HOST'].'/'.$res['goods_img'];
                    $map['class_img'] = "http://".$_SERVER['HTTP_HOST'].'/'.$res['goods_thumb'];
                }
            }

            $rel = M('goods_class')->where(array('class_id'=>$data['save_id']))->save($map);
            if($rel){
               // header("location: http://www.dzshop.net/Admin/Index/index.html");
                $this->success("修改成功！",U("GoodsClass/index"),1);
            }else{
                $this->error("修改失败！");
            }
        }

        $id = I('get.id');
        $data = M('Goods_class')->where(array('class_id'=>$id))->find();

        //上级ID
        $sname = M('Goods_class')->field('class_id,name')->where(array('class_id'=>$data['parent_class_id']))->find();
//dump($data);die;
        $this->assign('data',$sname);
        $this->assign('list',$data);

        $cateM = new Goods_classModel();

        $cate = $cateM->getCateTree();

        $this->assign('cate', $cate);
        $this->display();
    }

    public function dels(){
        $id = I('get.id');
        $map['state'] = 0;
        $rel = M('Goods_class')->where(array('class_id'=>$id))->save($map);
        if($rel){
            // header("location: http://www.dzshop.net/Admin/Index/index.html");
            $this->success("删除成功！",U("GoodsClass/index"),1);
        }else{
            $this->error("删除失败！");
        }


    }


}